<template>
  <div class="admin-index-page">
    <div class="content">
      <div class="header">
        <div class="left">
          <div class="drop">
            内容信息:
            <view class="mr-5"></view>
            <el-dropdown @command="handleClickDropDown">
              <el-button type="primary">
                <span>{{ categoryList[categoryIndex]?.catelog_name }}</span
                ><el-icon class="el-icon--right"><arrow-down /></el-icon>
              </el-button>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item
                    :command="index"
                    v-for="(item, index) in categoryList"
                    :key="index"
                    >{{ item.catelog_name }}</el-dropdown-item
                  >
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
          <!-- <div class="search ml-20">
            <div style="width: 60px">搜索</div>
            <el-input
              class="mx-5"
              v-model="articleParam.article_title"
              placeholder="请输入内容"
            ></el-input>
            <el-button type="primary" @click="fetchArticle()">搜索</el-button>
          </div> -->
        </div>
        <!-- <div class="functional">
          <el-button @click="handleClickFunctional('ADD')">添加</el-button>
        </div> -->
      </div>
      <div class="table">
        <el-table :border="true" :data="tableData" style="width: 100%">
          <el-table-column
            align="center"
            prop="article_id"
            label="ID"
            width="100"
          />
          <el-table-column
            prop="article_title"
            label="标题"
            width="180"
            align="center"
          >
            <template #default="{ $index }">
              <el-input
                type="textarea"
                v-model="tableData[$index].article_title"
                autosize
              ></el-input>
              <el-button
                class="mt-5"
                @click="
                  handleClickFunctionalRuning('ADD', $index, {
                    field: 'article_title',
                    value: tableData[$index].article_title,
                    id: tableData[$index].article_id,
                  })
                "
                >编辑</el-button
              >
            </template>
          </el-table-column>
          <el-table-column
            prop="ext_field3"
            label="标题(英文)"
            width="180"
            align="center"
          >
            <template #default="{ $index }">
              <el-input
                type="textarea"
                v-model="tableData[$index].ext_field3"
                autosize
              ></el-input>
              <el-button
                class="mt-5"
                @click="
                  handleClickFunctionalRuning('ADD', $index, {
                    field: 'ext_field3',
                    value: tableData[$index].ext_field3,
                    id: tableData[$index].article_id,
                  })
                "
                >编辑</el-button
              >
            </template>
          </el-table-column>
          <el-table-column prop="article_brief" label="正文" align="center">
            <template #default="{ $index }">
              <el-input
                type="textarea"
                v-model="tableData[$index].article_brief"
                autosize
              ></el-input>
              <el-button
                class="mt-5"
                @click="
                  handleClickFunctionalRuning('ADD', $index, {
                    field: 'article_brief',
                    value: tableData[$index].article_brief,
                    id: tableData[$index].article_id,
                  })
                "
                >编辑</el-button
              >
            </template>
          </el-table-column>
          <el-table-column prop="hbn" label="正文(英文)" align="center">
            <template #default="{ $index }">
              <el-input
                type="textarea"
                v-model="tableData[$index].hbn"
                autosize
              ></el-input>
              <el-button
                class="mt-5"
                @click="
                  handleClickFunctionalRuning('ADD', $index, {
                    field: 'hbn',
                    value: tableData[$index].hbn,
                    id: tableData[$index].article_id,
                  })
                "
                >编辑</el-button
              >
            </template>
          </el-table-column>
          <el-table-column prop="article_cover" label="单图" align="center">
            <template #default="{ $index }">
              <el-upload
                list-type="picture-card"
                class="avatar-uploader"
                :action="domain_path"
                :show-file-list="true"
                accept="image/*"
                :file-list="tableData[$index].article_cover"
                :on-success="
                  (response, file, fileList) =>
                    handleAvatarSuccess(
                      response,
                      file,
                      fileList,
                      $index,
                      'article_cover'
                    )
                "
                :on-remove="handleAvatarRemove"
                :limit="1"
                :on-exceed="handleAvatarExceed"
              >
              </el-upload>
              <el-button
                class="mt-5"
                @click="
                  handleClickFunctionalRuning('ADD', $index, {
                    field: 'article_cover',
                    value: forArrayToString(tableData[$index].article_cover),
                    id: tableData[$index].article_id,
                  })
                "
                >编辑</el-button
              >
            </template>
          </el-table-column>
          <!-- //article_label  rec_isdeleted-->
          <el-table-column prop="address" label="多图" align="center">
            <template #default="{ $index }">
              <el-upload
                list-type="picture-card"
                class="avatar-uploader"
                :action="domain_path"
                :show-file-list="true"
                accept="image/*"
                multiple
                :file-list="tableData[$index].article_share"
                :on-success="
                  (response, file, fileList) =>
                    handleAvatarSuccess(
                      response,
                      file,
                      fileList,
                      $index,
                      'article_share'
                    )
                "
                :on-remove="handleAvatarRemove"
                :on-exceed="handleAvatarExceed"
                :limit="10"
              >
              </el-upload>
              <el-button
                class="mt-5"
                @click="
                  handleClickFunctionalRuning('ADD', $index, {
                    field: 'article_share',
                    value: forArrayToString(tableData[$index].article_share),
                    id: tableData[$index].article_id,
                  })
                "
                >编辑</el-button
              >
            </template>
          </el-table-column>

          <el-table-column prop="article_label" label="小图" align="center">
            <template #default="{ $index }">
              <el-upload
                list-type="picture-card"
                class="avatar-uploader"
                :action="domain_path"
                :show-file-list="true"
                accept="image/*"
                :file-list="tableData[$index].article_label"
                :on-success="
                  (response, file, fileList) =>
                    handleAvatarSuccess(
                      response,
                      file,
                      fileList,
                      $index,
                      'article_label'
                    )
                "
                :on-remove="handleAvatarRemove"
                :limit="1"
                :on-exceed="handleAvatarExceed"
              >
              </el-upload>
              <el-button
                class="mt-5"
                @click="
                  handleClickFunctionalRuning('ADD', $index, {
                    field: 'article_label',
                    value: forArrayToString(tableData[$index].article_label),
                    id: tableData[$index].article_id,
                  })
                "
                >编辑</el-button
              >
            </template>
          </el-table-column>

          <el-table-column prop="rec_isdeleted" label="上下架" align="center">
            <template #default="{ $index }">
              <el-select
                v-model="tableData[$index].rec_isdeleted"
                placeholder="Select"
                size="large"
              >
                <el-option
                  v-for="item in salceOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
              <!-- <el-input
                type="textarea"
                v-model="tableData[$index].rec_isdeleted"
                autosize
              ></el-input> -->
              <el-button
                class="mt-5"
                @click="
                  handleClickFunctionalRuning('ADD', $index, {
                    field: 'rec_isdeleted',
                    value: tableData[$index].rec_isdeleted,
                    id: tableData[$index].article_id,
                  })
                "
                >编辑</el-button
              >
            </template>
          </el-table-column>
          <el-table-column
            prop="article_share_title"
            label="备注"
            align="center"
          >
            <!-- <template #default="{ $index }">
              <p>{{ tableData[$index].article_share_title }}</p>
            </template> -->
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>

  <el-dialog v-model="dialogVisible" title="Tips" width="500">
    <span>This is a message</span>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">Cancel</el-button>
        <el-button type="primary" @click="dialogVisible = false">
          Confirm
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, onMounted, computed } from "vue";
import RequestCommon from "@/api/common.js";
import RequestArticle from "@/api/article.js";
const domain_path = "http://ywapi.kfcit.com/fileupload/home";
const domain_pic = "http://ywapi.kfcit.com/";
const dialogVisible = ref(false);
const categoryList = ref([]);
const categoryIndex = ref(0);
const articleParam = ref({
  catelog_id: "",
  page: 1,
  limit: 20,
  article_title: "",
});
//  上下架
const salceOption = [
  {
    value: 0,
    label: "上架",
  },
  {
    value: 1,
    label: "下架",
  },
];
const singleData = ref([]);
const tableData = ref([]);

// handle
const handleClickDropDown = async (e) => {
  categoryIndex.value = e;
  articleParam.value.catelog_id =
    categoryList.value[categoryIndex.value].catelog_id;
  fetchArticle();
  console.log("handleClickDropDown", e);
};

const handleAvatarRemove = (e) => {
  console.log("==========handleAvatarRemove");
};

const handleAvatarExceed = (e) => {
  ElMessage("超过文件上传限制");
  console.log("=======-handleAvatarExceed", e);
};

const handleAvatarSuccess = (response, file, fileList, index, fileds) => {
  let files = response[0];
  tableData.value[index][fileds].push({
    url: forSplitHttp(files.path),
    ...files,
  });
  console.log("handleAvatarSuccess", tableData.value[index][fileds]);
};
// Functional
const FetchFunctionalAdd = async (param) => {
  await RequestArticle.updated(param)
    .then(() => {
      ElMessage({
        type: "success",
        message: "操作成功",
      });
    })
    .catch(() => {
      ElMessage({
        type: "error",
        message: "操作失败",
      });
    });
};

const handleClickFunctionalRuning = (type, index, param) => {
  if (!param.value) {
    return ElMessage({
      type: "warning",
      message: "值不能为空",
    });
  }
  switch (type) {
    case "ADD":
      FetchFunctionalAdd(param);
      break;
    case "EDIT":
      break;
    default:
      break;
  }
};
const handleClickFunctional = (type) => {
  console.log("====", type);
  switch (type) {
    case "ADD":
      handleVisible(dialogVisible);
      break;
    case "EDIT":
      handleVisible(dialogVisible);
      break;
    default:
      break;
  }
};

const handleVisible = (fileds) => {
  fileds.value = !fileds.value;
};

// component api
onMounted(() => {
  fetchInitializationData();
});

// 网络请求
// 请求分类
const fetchCategory = async () => {
  const { cats } = await RequestCommon.category();
  categoryList.value = cats;
  categoryList.value.unshift({ catelog_name: "全部", catelog_id: 1 });
};

const fetchArticle = async (articleParamOver) => {
  const param = {
    ...articleParam.value,
    ...articleParamOver,
  };
  let { cats } = await RequestArticle.article(param);
  tableData.value = cats.slice(0, 8);
  tableData.value.map((item) => {
    let article_cover_arr = item.article_cover
      ? item.article_cover.split(",")
      : [];
    let article_share_arr = item.article_share
      ? item.article_share.split(",")
      : [];
    let article_label_arr = item.article_label
      ? item.article_label.split(",")
      : [];
    item.article_cover = article_cover_arr.map((jitem) => {
      return { url: forSplitHttp(jitem) };
    });
    item.article_share = article_share_arr.map((jitem) => {
      return { url: forSplitHttp(jitem) };
    });
    item.article_label = article_label_arr.map((jitem) => {
      return { url: forSplitHttp(jitem) };
    });
  });
};
const fetchInitializationData = () => {
  fetchCategory();
  fetchArticle({
    catelog_id: 1,
    page: 1,
    limit: 10,
    article_title: "",
  });
};

// computed
const forArrayToString = computed(() => {
  return (array) => {
    const tmpArry = array.length ? [...array] : [];
    return tmpArry.map((item) => item.url).join();
  };
});
const forSplitHttp = (value) => {
  return value.includes("http") ? value : domain_pic + value;
};
</script>

<style lang="scss" scoped>
.admin-index-page {
  padding: 0px 150px;
  .content {
    .header {
      width: 100%;
      padding: 20px 0;
      display: flex;
      justify-content: space-between;
      .left {
        display: flex;

        .drop,
        .search {
          display: flex;
          align-items: center;
        }
        .search {
          width: 350px;
        }
      }
    }
    .table {
      width: 100%;
    }
  }
}
::v-deep .el-upload-list--picture-card .el-upload-list__item,
::v-deep .el-upload--picture-card {
  height: 120px;
  width: 120px;
}
</style>
